﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit.MEP.Modeling;
using YArchitech.Revit.MEP.Public;
using HYElectricCommon;
using HYRElectric.WirePlace;

namespace YArchitech.Revit.MEP.Command
{
	public partial class WireParamForm : System.Windows.Forms.Form
	{
		public Result ResultDlg { get; set; }

		public WireParamForm(Document m_doc, EnumFormMode formMode)
		{
			this.InitializeComponent();
			this.doc = m_doc;
			if (EnumFormMode.Non_mode == formMode)
			{
				this.btnOK.Visible = false;
				this.btnCancel.Visible = false;
				this.lblWire.Visible = true;
				this.cbxWire.Visible = true;
				this.cbxWire.Items.Add("倒角");
				this.cbxWire.Items.Add("弧线");
				this.cbxWire.SelectedIndex = 0;
				base.Height = 254;
				this.isNonMode = true;
				return;
			}
			this.btnOK.Visible = true;
			this.btnCancel.Visible = true;
			this.lblWire.Visible = false;
			this.cbxWire.Visible = false;
			base.Height = 260;
			this.btnOK.Location = new System.Drawing.Point(11, 200);
			this.btnCancel.Location = new System.Drawing.Point(112, 200);
			this.propertyGrid1.Location = new System.Drawing.Point(11, 11);
		}

		private void WireParamForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.FillPropertyGridByWireParam();
			this.LoadDlgData();
		}

		private void FillPropertyGridByWireParam()
		{
			ComboBoxItemWireType.Alist.Clear();
			this.wireTypeSet = this.GetWireType();
			if (this.wireTypeSet.Count == 0 || this.wireTypeSet == null)
			{
				MessageBox.Show("当前项目中未检测到导线类型", "盈建科提示");
				this.ResultDlg = Result.Failed;
				base.Close();
				return;
			}
			if (this.wireTypeSet != null && this.wireTypeSet.Count > 0)
			{
				foreach (Element element in this.wireTypeSet)
				{
					ComboBoxItemWireType.Alist.Add(element.Name);
				}
			}
			this.wireParameters.WireType = this.wireTypeSet[0].Name;
			ComboBoxItemLineType.Alist.Clear();
			LineTypeXMLManage lineTypeXMLManage = new LineTypeXMLManage(AssistFunc.GetAppRootPath() + "\\Data\\ElectricalData\\ElectricalLinetype.xml");
			this.lineTypeList = lineTypeXMLManage.GetLineType();
			if (this.lineTypeList.Count == 0 || this.lineTypeList == null)
			{
				MessageBox.Show("当前项目中未检测到导线线型", "盈建科提示");
				this.ResultDlg = Result.Failed;
				base.Close();
				return;
			}
			foreach (LineType lineType in this.lineTypeList)
			{
				ComboBoxItemLineType.Alist.Add(lineType.Title);
			}
			this.wireParameters.LineType = this.lineTypeList[0].Title;
			ComboBoxItemLayMode.Alist.Clear();
			ComboBoxItemLayMode.Alist.Add("BC");
			ComboBoxItemLayMode.Alist.Add("TC");
			ComboBoxItemLayMode.Alist.Add("CE");
			this.wireParameters.LayMode = "BC";
			this.propertyGrid1.SelectedObject = this.wireParameters;
		}

		public IList<Element> GetWireType()
		{
			return new FilteredElementCollector(this.doc).OfClass(typeof(WireType)).ToElements();
		}

		private void LoadDlgData()
		{
			string path = Path.GetTempPath() + "WireParamForm.dat";
			if (!File.Exists(path))
			{
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			string text = streamReader.ReadLine();
			IEnumerator<Element> enumerator = this.wireTypeSet.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Name == text.Trim())
					{
						this.wireParameters.WireType = text;
						break;
					}
				}
			}
			text = streamReader.ReadLine();
			this.wireParameters.LineType = text;
			text = streamReader.ReadLine();
			this.wireParameters.WireHot = Convert.ToInt32(text);
			text = streamReader.ReadLine();
			this.wireParameters.WireGround = Convert.ToInt32(text);
			text = streamReader.ReadLine();
			this.wireParameters.WireNeutral = Convert.ToInt32(text);
			text = streamReader.ReadLine();
			this.wireParameters.HotWireCrossSection = Convert.ToDouble(text);
			text = streamReader.ReadLine();
			this.wireParameters.WireCrossSection = Convert.ToDouble(text);
			text = streamReader.ReadLine();
			this.wireParameters.LayMode = text;
			if (this.cbxWire.Visible)
			{
				text = streamReader.ReadLine();
				this.cbxWire.SelectedIndex = Convert.ToInt32(text);
			}
			streamReader.Close();
			fileStream.Close();
		}

		public void SaveDlgData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "WireParamForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			string value = this.wireParameters.WireType;
			streamWriter.WriteLine(value);
			value = this.wireParameters.LineType;
			streamWriter.WriteLine(value);
			value = this.wireParameters.WireHot.ToString();
			streamWriter.WriteLine(value);
			value = this.wireParameters.WireGround.ToString();
			streamWriter.WriteLine(value);
			value = this.wireParameters.WireNeutral.ToString();
			streamWriter.WriteLine(value);
			value = this.wireParameters.HotWireCrossSection.ToString();
			streamWriter.WriteLine(value);
			value = this.wireParameters.WireCrossSection.ToString();
			streamWriter.WriteLine(value);
			value = this.wireParameters.LayMode;
			streamWriter.WriteLine(value);
			if (this.cbxWire.Visible)
			{
				value = this.cbxWire.SelectedIndex.ToString();
				streamWriter.WriteLine(value);
			}
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
            if (this.ResultDlg == Result.Failed)
			{
				MessageBox.Show("参数设置失败！", "盈建科提示");
				return;
			}
			WireParameters wireParameters = this.wireParameters;
			new LineType();
			string lineType = wireParameters.LineType;
			foreach (LineType lineType2 in this.lineTypeList)
			{
				if (lineType == lineType2.Title)
				{
					break;
				}
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void WireParamForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
			if (this.isNonMode)
			{
				this.isClose = true;
				int bVk = 27;
				int dwFlags = 0;
				int dwFlags2 = 2;
				WireParamForm.keybd_event((byte)bVk, 0, dwFlags, 0);
				WireParamForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
			}
		}

		private Document doc;

		public WireParameters wireParameters = new WireParameters();

		public List<LineType> lineTypeList = new List<LineType>();

		public bool isClose;

		private bool isNonMode;

		private IList<Element> wireTypeSet;
	}
}
